//
//  ALPOtherHotViewController.m
//  AlpFM
//
//  Created by KING on 16/3/1.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPOtherHotViewController.h"
#import "ALPOtherHotTableViewCell.h"
#import "ALPAlbumModel.h"
#import "MJRefresh.h"
#import <AFNetworking.h>
#import "ALPEditorPlayVC.h"

@interface ALPOtherHotViewController ()
<UITableViewDataSource, UITableViewDelegate
//, HFlisionAlbumCellDelegate
>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *allArray;
@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic,assign) NSInteger nextPage; //记录下一页
@property (nonatomic, strong) NSString *maxPageID;//最大页码
@end

@implementation ALPOtherHotViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.myTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.allArray = [NSMutableArray array];
    
    
    //铺tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT- 100) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.myTableView registerClass:[ALPOtherHotTableViewCell class] forCellReuseIdentifier:@"group"];
    
    
    //下拉刷新
    self.isUpLoading = NO;
    [self p_addHeader];
    [self p_addFooter];

}
#pragma mark -- 下拉刷新
- (void)p_addHeader{
    
    [self.myTableView addHeaderWithCallback:^{
        self.nextPage = 1;
        self.isUpLoading = NO;
        //重新请求数据
        [self p_getDataWithPage:self.nextPage];
    }];
    [self.myTableView headerBeginRefreshing];
}

#pragma mark -- 上拉刷新
- (void)p_addFooter{
    
    [self.myTableView addFooterWithCallback:^{
        self.nextPage++;
        self.isUpLoading = YES;
        [self p_getDataWithPage:self.nextPage];
        
    }];
}

#pragma mark -- 协议方法 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}

#pragma mark -- 协议方法 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ALPOtherHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"group"];
    cell.model = [self.allArray objectAtIndex:indexPath.row];
//    cell.MyDelegate = self;
    return cell;
}

#pragma mark -- 协议方法 高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 0.267*SCREEN_WIDTH;
}


#pragma mark --  获取数据
- (void)p_getDataWithPage:(NSInteger)page{
    //接收数据需要页码,标题和pageID(点击ID)
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=%@&pageId=%ld&pageSize=40", self.key, (long)page];
    
    //    NSLog(@"%@", url);
    //中文解码
    NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    
    [session GET:urlEncode parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
//        NSLog(@"成功");
#pragma mark -- 下拉清除
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.allArray removeAllObjects];
        }
        
#pragma mark -- 获取最大页码
        self.maxPageID = [responseObject objectForKey:@"maxPageId"];
        //                NSLog(@"页码最大数%@", self.maxPageID);
        
#pragma mark -- 加载数据
        if (page <= self.maxPageID.intValue) {
            NSMutableArray *array = [responseObject objectForKey:@"list"];
            for (NSMutableDictionary *dic in array) {
                ALPAlbumModel *model = [[ALPAlbumModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                
//                HFLisionAlbumModel *model = [[HFLisionAlbumModel alloc] initWithDictionary:dic];
                [self.allArray addObject:model];
            }
        }
        
#pragma mark --  停止菊花
        [self.myTableView reloadData];
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        NSLog(@"失败");
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
    }];
    
}

#pragma mark -- 点击分类的详情页(列表)会进入歌曲列表页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ALPAlbumModel *model = [self.allArray objectAtIndex:indexPath.row];
    ALPEditorPlayVC *vc = [[ALPEditorPlayVC alloc] init];
    
    vc.idid = model.trackID.intValue;
    [self presentViewController:vc animated:YES completion:^{
        
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
